package rest

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"monoblog/common"
	"monoblog/util"
	"net/http"
)

// PictureUpload 图片上传接口
func PictureUpload(ctx *gin.Context) {
	// 获取上传的文件
	file, err := ctx.FormFile("file")
	if err != nil {
		ctx.String(http.StatusBadRequest, fmt.Sprintf("获取文件失败: %v", err))
		return
	}
	logrus.Infof("上传的文件 filename= %s, size= %d", file.Filename, file.Size)

	linkUrl, err := util.UploadPicture(file.Filename, file)
	if err != nil {
		ctx.String(http.StatusInternalServerError, fmt.Sprintf("上传文件[%s]失败: %v", file.Filename, err))
		return
	}

	logrus.Infof("上传的文件 %s 已经保存成功。访问路径: %s", file.Filename, linkUrl)

	common.Success(ctx, linkUrl)
}
